﻿using RpcClient;
using RpcHelper;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class SignalStateService : ISignalStateService
        {
                private readonly IServerSignalStateCollect _SignalState;
                private readonly IServerCollect _Server;
                public SignalStateService (IServerSignalStateCollect signalState, IServerCollect server)
                {
                        this._Server = server;
                        this._SignalState = signalState;
                }
                public SignalState[] Gets (long serverId)
                {
                        ServerSignalState[] states = this._SignalState.Gets (serverId);
                        if (states.Length == 0)
                        {
                                return System.Array.Empty<SignalState>();
                        }
                        ServerConfigDatum[] servers = this._Server.Gets (states.ConvertAll (a => a.RemoteId));
                        return states.ConvertMap<ServerSignalState, SignalState> ((a, b) =>
                        {
                                ServerConfigDatum server = servers.Find (c => c.Id == a.RemoteId);
                                b.IsContainer = server.IsContainer;
                                b.IsOnline = server.IsOnline;
                                b.ServiceState = server.ServiceState;
                                b.ServerName = server.ServerName;
                                b.ServerId = server.Id;
                                return b;
                        });
                }
        }
}
